<template>
  <a-card :bordered="false" style="height:100%;padding-bottom:200px; ">

    <div class="table-page-search-wrapper" style="margin-left: 350px;">
      <div style="width: 600px;text-align: center;">
        <a-form-model ref="form" :model="model" :rules="validatorRules" layout="inline">

              <a-form-model-item label="试用时长(天)" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="trialDuration">
                <a-input-number v-model="model.trialDuration" name="trialDuration" :min="30" :max="1000000" :precision="0" placeholder="请输入试用时长(天)" style="width: 100%" />
              </a-form-model-item>

              <a-form-model-item label="激活时长(年)" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="activationDuration">
                <a-input-number v-model="model.activationDuration" placeholder="请输入激活时长(年)" style="width: 100%" />
              </a-form-model-item>

              <a-form-model-item label="安卓费用" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="androidCost">
                <a-input-number v-model="model.androidCost" placeholder="请输入安卓费用" style="width: 100%" />
              </a-form-model-item>

              <a-form-model-item label="IOS费用" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="iosCost">
                <a-input-number v-model="model.iosCost" placeholder="请输入IOS费用" style="width: 100%" />
              </a-form-model-item>

          <a-form-item :wrapperCol="{span: 19, offset: 5}">
            <a-button type="primary" @click="submitForm">提交</a-button>
          </a-form-item>
        </a-form-model>
      </div>
    </div>
  </a-card>
</template>

<script>
  import { httpAction, getAction } from '@/api/manage'
  export default {
    name: 'DeviceConfig',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          trialDuration:'',
          activationDuration:'',
          androidCost:'',
          iosCost:''
        },
        labelCol: { span: 5 },
        wrapperCol: {span: 19 },
        confirmLoading: false,
        validatorRules: {
          trialDuration: [
            { required: true,  message: '请输入试用时长(天)', trigger: 'blur' }
          ],
          activationDuration: [
            { required: true,  message: '请输入激活时长(年)', trigger: 'blur' }
          ],
          androidCost: [
            { required: true,  message: '请输入Android费用', trigger: 'blur' }
          ],
          iosCost: [
            { required: true,  message: '请输入IOS费用', trigger: 'blur' }
          ],
        },
        url: {
          add: "/device/deviceConfig/add",
          edit: "/device/deviceConfig/edit",
          queryById: "/device/deviceConfig/queryById",
          quertyConfigInfo: "/device/deviceConfig/quertyConfigInfo"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
      //备份model原始值
      //this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    mounted () {
      this.getConfigInfo()
    },
    methods: {
      getConfigInfo() {
        this.$http.get('/device/deviceConfig/quertyConfigInfo')
          .then(res => {
            if(res.result!=null){
              this.model = res.result
            }

          })
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
              method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>

<style scoped>

</style>